from train import  *
import tensorflow as tf
from sklearn import *
import matplotlib.pyplot as plt
model_json=model.to_json()

with open(r"model.json","w") as f:
    f.write(model_json)
model.save_weights("./model.h5")

my_model=tf.keras.models.model_from_json(open("model.json").read())
my_model.load_weights("model.h5")
y_pred=my_model.predict(x_test)


print("a_err:>>",metrics.mean_absolute_error(y_pred,y_test))
print("Max_error:>>",metrics.max_error(y_pred,y_test))
print("MSE:>>",metrics.mean_squared_error(y_pred,y_test))
print("log_err:>>",metrics.mean_squared_log_error(y_pred,y_test))
print("median_err:>>",metrics.median_absolute_error(y_pred,y_test))


plt.rcParams["font.sans-serif"]=["SimHei"]
plt.rcParams["axes.unicode_minus"]=False

pred=model.predict(x_test)
acc=y_test
pre=pred
fig=plt.figure(figsize=(20,10))
ax1,ax2,ax3=fig.subplots(3,1)

ax1.set_title("实际值与预测值折线图")
ax1.set_xlabel("ID")
ax1.set_ylabel("Y")
ax1.plot(acc,color="r",label="acc")
ax1.plot(pre,color="b",label="pre")

ax2.set_title("实际值折线图")
ax2.set_xlabel("ID")
ax2.set_ylabel("Y")
ax2.plot(acc,color="r",label="acc")
# ax2.plot(pre,color="b",label="pre")

ax3.set_title("预测值折线图")
ax3.set_xlabel("ID")
ax3.set_ylabel("Y")
ax3.plot(acc,color="b",label="pre")

fig.suptitle("验证结果")

#plt.savefig("验证结果.png")
plt.show()
